package com.example.demo.controller.advice;

import com.example.demo.result.JsonResult;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhanpengguo
 * @date 2019-07-03 17:37
 */
@RestControllerAdvice
public class JsonExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public Object errorHandler(HttpServletRequest request, HttpServletResponse response, Exception e){
        e.printStackTrace();
        if (e instanceof UnauthorizedException){
            return new JsonResult("401","无权限",e.getMessage()).toString();
        }
        if (e instanceof UnauthenticatedException){
            return new JsonResult("401","身份未验证",e.getMessage()).toString();
        }
        return new JsonResult("500","系统错误",e.getMessage()).toString();
    }
}
